using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class UIUtils
{
    public static Sprite LoadByIO(string path)
    {
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        fileStream.Seek(0, SeekOrigin.Begin);

        byte[] bytes = new byte[fileStream.Length];

        fileStream.Read(bytes, 0, (int)fileStream.Length);

        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;

        Texture2D texture = new Texture2D(1, 1);
        texture.LoadImage(bytes);
        string[] strs = path.Split('\\');
        texture.name = strs[strs.Length - 1];

        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        return sprite;
    }

    /// <summary>
    /// 只能在渲染帧调用
    /// </summary>
    /// <returns></returns>
    public static Texture2D ScreenShot_ReadPixels(Rect rect)
    {
        Texture2D texture2D = new Texture2D((int)rect.width, (int)rect.height);
        texture2D.ReadPixels(rect, 0, 0, false);
        return texture2D;
    }
}
